package edu.thu.studio.ormdesigner.editor.action;

import org.eclipse.gef.commands.Command;
import org.eclipse.ui.IWorkbenchPart;

import edu.thu.studio.ormdesigner.editor.command.CreateTableNodeShortCutCommand;
import edu.thu.studio.ormdesigner.editor.command.CreateViewNodeShortCutCommand;
import edu.thu.studio.ormdesigner.model.diagram.node.Node;
import edu.thu.studio.ormdesigner.model.diagram.node.TableNode;
import edu.thu.studio.ormdesigner.model.diagram.node.ViewNode;

public class ShortCutAction extends AbstractSelectionAction {
    
    public static final String ID = ShortCutAction.class.getName();
    
    public ShortCutAction(IWorkbenchPart part) {
        super(part);
        this.setId(ID);
    }
    
    @Override
    public void run() {
        try {
            execute(createCommand());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    @Override
    protected boolean calculateEnabled() {
        if (CopyManager.canPasteShortCut(this.getCurrentDiagram())) {
            return true;
        }
        return false;
    }
    
    private Command createCommand() {
        if (!calculateEnabled()) {
            return null;
        }
        
        Node node = CopyManager.pasteAsShortCut();
        
        if (node instanceof ViewNode) {
            return new CreateViewNodeShortCutCommand(getCurrentDiagram(), (ViewNode) node);
        } else {
            return new CreateTableNodeShortCutCommand(getCurrentDiagram(), (TableNode) node);
        }
    }
    
}
